const {DateTime} = require("luxon");
const fetch = require('node-fetch')
module.exports.handler = function (event, context, callback) {
  const ticketId = process.env['ticketId'];
  const seatId = process.env['seatId']
  const cookie = process.env['cookie']
  const minute = process.env['saleTimeMinute']
  const hour = process.env['saleTimeHour']
  const second = process.env['saleTimeSecond']
  const now = DateTime.now();
  const tm = DateTime.local(now.year, now.month, hour, minute, second)
  if (now.ts < tm.ts) {
    console.log('preheat')
  } else {
    fetch("https://m.48.cn/TOrder/add", {
      "headers": {
        "accept": "application/json, text/javascript, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "pragma": "no-cache",
        "sec-ch-ua": "\"Chromium\";v=\"91\", \" Not;A Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?1",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "x-requested-with": "XMLHttpRequest",
        "cookie": cookie
      },
      "referrer": "https://m.48.cn/tickets/item/" + ticketId,
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": "id=" + ticketId + "&num=1&seattype=" + seatId + "&brand_id=5&r=0.8889058443104612&choose_times_end=-1",
      "method": "POST",
      "mode": "cors"
    }).then(function (response) {
      response.json().then(function (data) {
        console.log(data)
      })
    })
  }
  callback(null, '');
};

